package mystack;/*
 *@ 代阳敲的专属代码
 **/

import java.lang.reflect.Array;
import java.util.Arrays;

public class MyStack {
    public int [] elem;
    public int usedSize;

    public MyStack() {
        this.elem=new int[10];
    }
    public void push(int val) {
        if(ifFull()){
            elem=expansion(elem);
        }
        elem[usedSize++] =val;
    }
    public  int pop() {
        if(isEmpty()) {
            throw  new StackException("栈为空");
        }
        int oldVal=elem[usedSize-1];
        usedSize--;
        return oldVal;
    }
    public int peek() {
        if(isEmpty()) {
            throw  new StackException("栈为空");
        }
        return elem[usedSize-1];
    }
    private boolean isEmpty() {
        return  usedSize==0;
    }

    private int[] expansion(int [] elem) {
        return Arrays.copyOf(elem,elem.length*2);
    }
    private boolean ifFull() {
        return elem.length==usedSize;
    }
}
